package com.jztd.servone.invoice.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jztd.platfrom.base.exception.ResourceNotFoundException;
import com.jztd.servone.invoice.dto.OrderDTO;
import com.jztd.servone.invoice.dto.ZGZZOrderDTO;
import com.jztd.servone.emun.ClientTypeEnum;
import com.jztd.servone.invoice.param.OrderInterfaceConfigParam;
import com.jztd.servone.invoice.param.OrderParam;
import com.jztd.servone.invoice.po.BusinessConfig;
import com.jztd.servone.invoice.service.BusinessConfigService;
import com.jztd.servone.invoice.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**自贡车站获取订单信息*/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZGZZOrderServiceImpl implements OrderService {
    private final BusinessConfigService businessConfigService;

    @Override
    public OrderDTO getOrderInfo(BusinessConfig businessConfig,OrderParam param) {
        if(ObjectUtil.isEmpty(param.getInvoiceCodes())){
            throw new ResourceNotFoundException("暂无订单信息");
        }
        OrderInterfaceConfigParam interfaceConfigParam = businessConfig.getOrdeDetailsrUrl();
        String url = businessConfig.getUrl();
        if(ObjectUtil.isEmpty(interfaceConfigParam)){
        }
        if(ObjectUtil.isNotEmpty(interfaceConfigParam.getMethod())){
            url += interfaceConfigParam.getMethod();
        }
        String result =null;
        if(ObjectUtil.isNotEmpty(interfaceConfigParam.getRequestType()) && interfaceConfigParam.getRequestType().equals("POST")){
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("EInvoiceID", param.getInvoiceCodes());
            result = HttpUtil.post(url, JSONUtil.toJsonStr(paramMap));
        }else {
            result = HttpUtil.get(url+"?EInvoiceID="+param.getInvoiceCodes());
        }
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if(ObjectUtil.isEmpty(jsonObject.getStr("data"))){

        }
        List<ZGZZOrderDTO> orderDTO = Convert.toList(ZGZZOrderDTO.class,jsonObject.get("data"));
        OrderDTO order = orderDTO.get(0).getOrderInfo();
        order.setInvoicePartyCode(businessConfig.getPartyConfigCode());
        return order;
    }

    @Override
    public List<OrderDTO> getInvoiceOrderList(BusinessConfig businessConfig,OrderParam param) {
        return List.of();
    }

    @Override
    public void updateOrderInfo(BusinessConfig businessConfig,String ticketId, String invoiceStatus) {
        log.warn("未实现同步发票状态");
    }

    @Override
    public String getServiceType() {
        return ClientTypeEnum.BST_ZGZZ.name();
    }
}
